package com.mobi.Survey.map;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.mobi.Survey.lib.activities.Messages;
import com.mobi.Survey.lib.utils.Helper;

public class AddBusinessScreen extends MyTab {

	private View containerAddress;
	private EditText editAddEmail;
	private EditText editAddWeb;
	private EditText editAddBusName;
	private EditText editAddPhone;
	private EditText editAddNotes;
	private EditText editAddStreetNum;
	private EditText editAddStreetName;
	private EditText editAddCity;
	private EditText editAddState;
	private EditText editAddCountry;
	private EditText editAddZip;
	private String selectedCategories = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initUiVars() {
		containerAddress = findViewById(R.id.containerAddress);
		editAddEmail = (EditText) findViewById(R.id.editAddEmail);
		editAddWeb = (EditText) findViewById(R.id.editAddWeb);
		editAddBusName = (EditText) findViewById(R.id.editAddBusName);
		editAddPhone = (EditText) findViewById(R.id.editAddPhone);
		editAddNotes = (EditText) findViewById(R.id.editAddNotes);
		editAddStreetNum = (EditText) findViewById(R.id.editAddStreetNum);
		editAddStreetName = (EditText) findViewById(R.id.editAddStreetName);
		editAddCity = (EditText) findViewById(R.id.editAddCity);
		editAddState = (EditText) findViewById(R.id.editAddState);
		editAddCountry = (EditText) findViewById(R.id.editAddCountry);
		editAddZip = (EditText) findViewById(R.id.editAddZip);

	}

	public void onClick(View v) {
		if (v.getId() == R.id.llAddress) {
			toggleAddressView();

		} else if (v.getId() == R.id.llCategories) {

			onSelectCategoriesClick();
		} else if (v.getId() == R.id.btnAddBusiness) {
			onAddBusiness();
		}
	}

	private void onAddBusiness() {
		// = editAddEmail.getText().toString();;
		// = editAddWeb.getText().toString();;
		// = editAddBusName.getText().toString();
		// = editAddPhone.getText().toString();
		// = editAddNotes.getText().toString();
		// = editAddStreetNum.getText().toString();
		// = editAddStreetName.getText().toString();
		// = editAddTown.getText().toString();
		// = editAddCity.getText().toString();
		// = editAddState.getText().toString();
		// = editAddCountry.getText().toString();
		// = editAddZip.getText().toString();

		new AddBusinessTask().execute();
	}

	private void onSelectCategoriesClick() {

		Intent i = new Intent(this, SelectCategoryScreen.class);
		i.putExtra("selected_categories", selectedCategories);
		startActivityForResult(i, 100);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == 100 && resultCode == RESULT_OK) {
			selectedCategories = (String) data.getExtras().get("result");
			if (selectedCategories.length() > 0) {
				selectedCategories = selectedCategories.substring(1,
						selectedCategories.length() - 1);
			}

		}

	}

	private void toggleAddressView() {

		int visibility = View.VISIBLE;
		if (containerAddress.getVisibility() == View.VISIBLE) {
			visibility = View.GONE;
		}
		containerAddress.setVisibility(visibility);

	}

	@Override
	protected int getLayoutId() {

		return R.layout.add_business;
	}

	private class AddBusinessTask extends AsyncTask<Integer, Integer, Integer> {
		protected ProgressDialog progressDialog = null;
		private String url;

		@Override
		protected void onPostExecute(Integer result) {

			super.onPostExecute(result);
			if (result == 1) {
				showLongToast("Data sent.");
				resetFields();
			} else {
				showAlert("Error: Can not add business please retry later");
			}
			progressDialog.dismiss();
			progressDialog = null;

		}

		@Override
		protected void onPreExecute() {

			super.onPreExecute();

			String email = URLEncoder.encode(editAddEmail.getText().toString());
			String web = URLEncoder.encode(editAddWeb.getText().toString());

			if (email.length() == 0 || web.length() == 0) {
				cancel(true);
				showAlert("Web and Email address are required.");
				return;
			}
			String busName = URLEncoder.encode(editAddBusName.getText()
					.toString());
			String phone = URLEncoder.encode(editAddPhone.getText().toString());
			String notes = URLEncoder.encode(editAddNotes.getText().toString());
			String streetNum = URLEncoder.encode(editAddStreetNum.getText()
					.toString());
			String streetName = URLEncoder.encode(editAddStreetName.getText()
					.toString());
			String city = URLEncoder.encode(editAddCity.getText().toString());
			String state = URLEncoder.encode(editAddState.getText().toString());
			String country = URLEncoder.encode(editAddCountry.getText()
					.toString());
			String zip = URLEncoder.encode(editAddZip.getText().toString());

			progressDialog = ProgressDialog.show(AddBusinessScreen.this, "",
					"Adding Business");
			// =%s&amp;=%s&amp;=%s&amp;=%s&amp;=%s&amp;=%d&amp;=%d&amp;=%s&amp;=%s&amp;=%s&amp;=%s&amp;=%s&amp;Notes=%s

			url = getResources().getString(R.string.addBusiness_ws, busName,
					URLEncoder.encode(selectedCategories), web, phone, email,
					1 + "", streetNum, streetName, city, state, country, zip,
					notes);
		}

		@Override
		protected void onProgressUpdate(Integer... values) {

			super.onProgressUpdate(values);

		}

		@Override
		protected Integer doInBackground(Integer... params) {
			if (Helper.isNetworkAvailable(AddBusinessScreen.this)) {

				try {
					if (isCancelled()) {
						return null;
					}
					InputStream is = Helper.performGetOperationI(url);
					return 1;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				showShortToastOnUiThread(AddBusinessScreen.this, Messages.NO_INTERNET);
			}
			return null;

		}
	}

	private void resetFields() {
		containerAddress.setVisibility(View.GONE);

		editAddEmail.setText("");
		editAddWeb.setText("");
		editAddBusName.setText("");
		editAddPhone.setText("");
		editAddNotes.setText("");
		editAddStreetNum.setText("");
		editAddStreetName.setText("");
		// editAddTown.setText("");
		editAddCity.setText("");
		editAddState.setText("");
		editAddCountry.setText("");
		editAddZip.setText("");
		selectedCategories = "";
	}

}
